
package com.mephi.controller.finder;


import java.util.Map;
import com.mephi.exception.IllegalGameStateException;
import com.mephi.gui.Field;
import com.mephi.interfaces.Chessboard;
import com.mephi.interfaces.Game;
import com.mephi.interfaces.PieceMoveFinder;
import com.mephi.model.Move;
import com.mephi.model.PieceMovement;
import com.mephi.util.AuxClass;
import com.mephi.util.PieceColor;


/**
 * A default implementation. Creates long algebraic notation.
 * 
 * @author mephisto
 * 2011-06-23
 */
public class PawnMoveFinderImpl implements PieceMoveFinder
{
	/**
	 * 
	 * @param gameModel the model of the game to set
	 * @param chessboard the reference to chessboard to set
	 */
	public PawnMoveFinderImpl(Game<Move> gameModel, Chessboard chessboard)
	{
		setGameModel(gameModel);
		setChessboard(chessboard);
	}
	
	/**
	 * 
	 * @return the gameModel
	 */
	public Game<Move> getGameModel()
	{
		return gameModel;
	}

	/**
	 * 
	 * @param gameModel to the gameModel to set
	 * @throws IllegalArgumentException if gameModel is null
	 */
	public void setGameModel(Game<Move> gameModel)
	{
		AuxClass.checkArgument(gameModel, new IllegalArgumentException("gameModel is null!"));
		this.gameModel = gameModel;
	}

	/**
	 * 
	 * @return the chessboard
	 */
	public Chessboard getChessboard()
	{
		return chessboard;
	}

	/**
	 * 
	 * @param chessboard to the chessboard to set
	 * @throws IllegalArgument Exception if chessboard is null 
	 * @throws map which describes all fields doesn't have exactly
	 * 		   64 keys used for chess notation
	 */
	public void setChessboard(Chessboard chessboard)
	{
		AuxClass.checkArgument(chessboard, new IllegalArgumentException("chessboard is null!"));
		Map<String, Field> chessFieldMap = chessboard.getChessFieldsMap();
		if (chessFieldMap.size() != 64)
			new IllegalGameStateException("Chessboard has field " + chessFieldMap.size() + " fields. It should have 64!");
		for(int i=0, size = cols.length; i<size; i++)
			for(int j=0; j<size; j++)
			{
				StringBuilder strField = new StringBuilder(cols[i]).append(j);
				Field field = chessFieldMap.get(strField);
				if (field == null)
					new IllegalGameStateException("Chessboard has field " + strField + "which is null!");				
			}
		this.chessboard = chessboard;
	}
	
	/**
	 * Returns PieceMovement in the long notation of chess move. It converts
	 * short notation to long.
	 * 
	 * @param moveNumber the number of move to set
	 * @param pieceColor the PieceColor to set
	 * @return the long notation of chess move
	 * 
	 * @throws IllegalGameStateException if game is illegal state
	 * (calculating long algebraic move is not possible)
	 */
	@Override
	public PieceMovement find(int moveNumber, PieceColor pieceColor) 
			throws IllegalGameStateException
	{
		PieceMovement pM = null;
		String strMove = null;
		if (pieceColor.equals(PieceColor.WHITE))
			strMove = gameModel.get(moveNumber-1).getWhiteMove();
		else
			strMove = gameModel.get(moveNumber-1).getBlackMove();
		return null;
	}
	
	//return the number which indicates column or -1 if nothing was found
	private int getNumberFromCol(String col)
	{
		for(int i=0, size = cols.length; i<size; i++)
			if (col.equals(cols[i]))
				return i+1;
		return -1;
	}

	
	private static final String[] cols = {"a", "b", "c", "d", "e", "f", "g", "h"};
	private Game<Move> gameModel;
	private Chessboard chessboard;
}
